<? /*
	// File:	schedbackups.inc.phps
	// Purpose:	scheduled backups
	// Author:	Peter Drake <peterdrake@241computers.com>
	*/
$SQL = "SELECT id, domain, files, email, databases, remote, local, ftpserver, username, directory, DECODE(password,'".$cfg['key']."') AS password FROM backups WHERE recurrance = '" . $schedbackups . "'";
$backupresult = mysql_query($SQL);
//Check first to make sure we actually got some kind of data. Otherwise do nothing.
if ($backupresult != false) {
	$num_rows = mysql_num_rows($backupresult);
	if ($num_rows != 0) {
		for ($i = 0; $domainrow = mysql_fetch_array($backupresult); $i++) {
			
		if ($domainrow["domain"] != "") {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Started for ".$domainrow["domain"],0,$echo);
			$filename = $domainrow["domain"]."-".time().".tar";
			$compressedFilename = $filename.".gz";
			if ($domainrow["files"] == 1) {
				chdir($cfg['webdir'].'/'.$domainrow["domain"]);
				exec($cfg['prog']['tar'].' -chf /tmp/'.$filename.' `ls | '.$cfg['prog']['grep'].' -v _backups`');
			} else {
				exec('touch /tmp/'.$filename);
			}
			if ($domainrow["mail"] == 1) {
				$SQL = "SELECT id FROM domains WHERE domain = '" . $domainrow["domain"] . "'";
				$dres = mysql_query($SQL);
				if ($drow = mysql_fetch_array($dres)) {
					$SQL = "SELECT username FROM users WHERE id = ".$drow["id"];
					$ures = mysql_query($SQL);
					
					for($j = 0; $row = mysql_fetch_array($ures); $j++) {
						exec('tar -hrf /tmp/'.$filename.' /var/mail/'.$row["username"]);
					}
				}
			}
			// Compress file
			exec('gzip /tmp/'.$filename);
		
			// Perform local backup
			if ($domainrow["local"] == 1) {
				if (copy('/tmp/'.$compressedFilename, $cfg['webdir'].'/'.$domainrow["domain"].'/_backups/'.$compressedFilename)) {
					chgrp($cfg['webdir'].'/'.$domainrow["domain"].'/_backups/'.$compressedFilename, 'g'.$domainrow["id"]);
					chown($cfg['webdir'].'/'.$domainrow["domain"].'/_backups/'.$compressedFilename, $cfg['httpd_user']);
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Successful for ".$domainrow["domain"],0,$echo);
				} else {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Failed for ".$domainrow["domain"],0,$echo);
				}
			}	
		
			// Perform remote backup
			if ($domainrow["remote"] == 1) {
				$conn_id = ftp_connect($domainrow["ftpserver"]);
				if (!$conn_id) {
					webcp_log(3,0,'schedbackups.inc.phps',"Note: Connection failed to ".$domainrow["ftpserver"],0,$echo);
				}
				$login_result = ftp_login($conn_id, $domainrow["username"], $domainrow["password"]);
				if ($domainrow["directory"] != "") {
					$dir_result = ftp_chdir($conn_id, $domainrow["directory"]);
				}
				if (ftp_put($conn_id, $compressedFilename, '/tmp/'.$compressedFilename, FTP_BINARY)) {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Successful for ".$domainrow["domain"],0,$echo);
				} else {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Failed for ".$domainrow["domain"],0,$echo);
				}
				ftp_close($conn_id);
			}
			
			// Cleanup files
			unlink('/tmp/'.$compressedFilename);
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Completed for ".$domainrow["domain"],0,$echo);
		}
		
		// Perform database backup
		if ($domainrow["databases"] == 1) {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Started Databases",0,$echo);
			
			$filename = "Databases_Backup_".time().".tar.gz";
			$mysql_filename = 'mysql_databases.sql';
			$pgsql_filename = 'pgsql_databases.sql';
			
			if ($cfg['user_mysql'] == true) {
				$checkDB = $cfg['prog']['mysqlcheck'].' --analyze --optimize --check-only-changed --auto-repair --all-databases -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
				exec($checkDB);
				$dumpDB = $cfg['prog']['mysqldump'].' -Ar/tmp/'.$mysql_filename.' -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
				exec($dumpDB);
			}
			if ($cfg['user_pgsql'] == true) {
				$dumpDB = 'export PGPASSWORD='.$cfg['pgsql_pass'].';'.$cfg['prog']['pgsqldumpall'].' > /tmp/'.$pgsql_filename.' -U'.$cfg['pgsql_user'];
				exec($dumpDB);
			}
			
			if (($cfg['user_pgsql'] == true) && ($cfg['user_mysql'] == true)) {
				$backup_files = '/tmp/'.$mysql_filename.' /tmp/'.$pgsql_filename;
			}	elseif ($cfg['user_mysql'] == true) {
				$backup_files = '/tmp/'.$mysql_filename;
			}	elseif ($cfg['user_pgsql'] == true) {
				$backup_files = '/tmp/'.$pgsql_filename;
			}

			// Create compressed db backup archive
			exec($cfg['prog']['tar'].' -chzf /tmp/'.$filename.' '.$backup_files);
		
			// Perform local db backup
			if ($domainrow["local"] == 1) {
				if (copy('/tmp/'.$filename, $cfg['basedir'].'/_backups/'.$filename)) {
					chown($cfg['basedir'].'/_backups/'.$filename, $cfg['httpd_user']);
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Databases Successful",0,$echo);
				} else {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Databases Failed",0,$echo);
				}
			}
			
			// Perform remote db backup
			if ($domainrow["remote"] == 1) {
				$conn_id = ftp_connect($domainrow["ftpserver"]);
				if (!$conn_id) {
					webcp_log(3,0,'schedbackups.inc.phps',"Note: Connection failed to ".$domainrow["ftpserver"],0,$echo);
				}
				$login_result = ftp_login($conn_id, $domainrow["username"], $domainrow["password"]);
				if ($domainrow["directory"] != "") {
					$dir_result = ftp_chdir($conn_id, $domainrow["directory"]);
				}
				if (ftp_put($conn_id, $filename, '/tmp/'.$filename, FTP_BINARY)) {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Databases Successful",0,$echo);
				} else {
					webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Databases Failed",0,$echo);
				}
				ftp_close($conn_id);
			}
			
			// Cleanup - Remove compressed tmp file
			unlink('/tmp/'.$filename);
		
			// Cleanup sql files
			$backup_files = explode(' ', $backup_files);
			foreach($backup_files as $file) {
				unlink($file);
			}
			
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Completed Databases",0,1);
			}
		}
	}
}
webcp_log(3,0,'schedbackups.inc.phps',$schedbackups." scheduled backups ran.",0);
?>
